package cn.wolfcode.trip.core.service.impl;

import cn.wolfcode.trip.core.domain.StrategyCatalog;
import cn.wolfcode.trip.core.domain.StrategyComment;
import cn.wolfcode.trip.core.domain.Tag;
import cn.wolfcode.trip.core.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.core.mapper.TagMapper;
import cn.wolfcode.trip.core.query.QueryObject;
import cn.wolfcode.trip.core.service.IStrategyCommentService;
import cn.wolfcode.trip.core.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Autowired
    private TagMapper tagMapper;

    public void saveOrUpdate(StrategyComment strategyComment, String[] tags) {
        if(strategyComment.getId()!=null){
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        }else{
            // 设置评论人
            strategyComment.setUser(UserContext.getCurrentUser());
            strategyCommentMapper.insert(strategyComment);
            for (String tagName : tags) {
                // 保存标签
                Tag tag = new Tag();
                tag.setName(tagName);
                tagMapper.insert(tag);
            // 处理中间表
            strategyCommentMapper.insertRelation(strategyComment.getId(),tag.getId());
            }
        }
    }

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyCatalog> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo(list);
    }
}
